<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <title>Gogs中文文档 - 书栈(BookStack.CN)</title>
    <link href="editormd.css" rel="stylesheet">
</head>
<body>
    <h1 id="article-title">2.5 源码安装</h1>
    <div class="article-body markdown-body editormd-preview-container"  id="page-content">
        <div class="markdown-toc editormd-markdown-toc"><ul class="markdown-toc-list"><li><a class="toc-level-1" href="#源码安装" level="1">源码安装</a><ul><li><a class="toc-level-2" href="#安装依赖" level="2">安装依赖</a><ul><li><a class="toc-level-3" href="#基本依赖" level="3">基本依赖</a></li><li><a class="toc-level-3" href="#第三方包" level="3">第三方包</a></li></ul></li><li><a class="toc-level-2" href="#安装 Go 语言" level="2">安装 Go 语言</a><ul><li><a class="toc-level-3" href="#下载" level="3">下载</a></li><li><a class="toc-level-3" href="#设置环境" level="3">设置环境</a></li></ul></li><li><a class="toc-level-2" href="#安装 Gogs" level="2">安装 Gogs</a><ul><li><a class="toc-level-3" href="#构建 &lt;code&gt;develop&lt;/code&gt; 分支版本" level="3">构建 <code>develop</code> 分支版本</a></li><li><a class="toc-level-3" href="#测试安装" level="3">测试安装</a></li><li><a class="toc-level-3" href="#使用标签构建" level="3">使用标签构建</a><ul></ul></li></ul></li></ul></li></ul></div><h1 id="h1-u6E90u7801u5B89u88C5"><a name="源码安装" class="reference-link"></a><span class="header-link octicon octicon-link"></span>源码安装</h1><h2 id="h2-u5B89u88C5u4F9Du8D56"><a name="安装依赖" class="reference-link"></a><span class="header-link octicon octicon-link"></span>安装依赖</h2><h3 id="h3-u57FAu672Cu4F9Du8D56"><a name="基本依赖" class="reference-link"></a><span class="header-link octicon octicon-link"></span>基本依赖</h3><ul>
<li><a href="http://golang.org">Go 语言</a>：版本 &gt;= 1.6</li></ul>
<h3 id="h3-u7B2Cu4E09u65B9u5305"><a name="第三方包" class="reference-link"></a><span class="header-link octicon octicon-link"></span>第三方包</h3><p>请通过 <a href="https://github.com/gogits/gogs/blob/master/.gopmfile">gopmfile</a> 查看完整的第三方包依赖列表。一般情况下只有在您为 Gogs 制作构建包的时候才会用到。</p>
<h2 id="h2--go-"><a name="安装 Go 语言" class="reference-link"></a><span class="header-link octicon octicon-link"></span>安装 Go 语言</h2><p>如果您的系统已经安装要求版本的 Go 语言，可以跳过此小节。</p>
<h3 id="h3-u4E0Bu8F7D"><a name="下载" class="reference-link"></a><span class="header-link octicon octicon-link"></span>下载</h3><p>您可以通过以下方式安装 Go 语言到 <code>/home/git/local/go</code> 目录：</p>
<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code class="lang-sh"><span class="pln">sudo su </span><span class="pun">-</span><span class="pln"> git</span></code></li><li class="L1"><code class="lang-sh"><span class="pln">cd </span><span class="pun">~</span></code></li><li class="L2"><code class="lang-sh"><span class="com"># create a folder to install &#39;go&#39;</span></code></li><li class="L3"><code class="lang-sh"><span class="pln">mkdir </span><span class="kwd">local</span></code></li><li class="L4"><code class="lang-sh"><span class="com"># Download go (change go$VERSION.$OS-$ARCH.tar.gz to the latest release)</span></code></li><li class="L5"><code class="lang-sh"><span class="pln">wget https</span><span class="pun">://</span><span class="pln">storage</span><span class="pun">.</span><span class="pln">googleapis</span><span class="pun">.</span><span class="pln">com</span><span class="pun">/</span><span class="pln">golang</span><span class="pun">/</span><span class="pln">go$VERSION</span><span class="pun">.</span><span class="pln">$OS</span><span class="pun">-</span><span class="pln">$ARCH</span><span class="pun">.</span><span class="pln">tar</span><span class="pun">.</span><span class="pln">gz</span></code></li><li class="L6"><code class="lang-sh"><span class="com"># expand it to ~/local</span></code></li><li class="L7"><code class="lang-sh"><span class="pln">tar </span><span class="pun">-</span><span class="pln">C </span><span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">git</span><span class="pun">/</span><span class="kwd">local</span><span class="pln"> </span><span class="pun">-</span><span class="pln">xzf go$VERSION</span><span class="pun">.</span><span class="pln">$OS</span><span class="pun">-</span><span class="pln">$ARCH</span><span class="pun">.</span><span class="pln">tar</span><span class="pun">.</span><span class="pln">gz</span></code></li></ol></pre>
<h3 id="h3-u8BBEu7F6Eu73AFu5883"><a name="设置环境" class="reference-link"></a><span class="header-link octicon octicon-link"></span>设置环境</h3><p>请设置和您系统环境对应的路径：</p>
<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code class="lang-sh"><span class="pln">sudo su </span><span class="pun">-</span><span class="pln"> git</span></code></li><li class="L1"><code class="lang-sh"><span class="pln">cd </span><span class="pun">~</span></code></li><li class="L2"><code class="lang-sh"><span class="pln">echo </span><span class="str">&#39;export GOROOT=$HOME/local/go&#39;</span><span class="pln"> </span><span class="pun">&gt;&gt;</span><span class="pln"> $HOME</span><span class="pun">/.</span><span class="pln">bashrc</span></code></li><li class="L3"><code class="lang-sh"><span class="pln">echo </span><span class="str">&#39;export GOPATH=$HOME/go&#39;</span><span class="pln"> </span><span class="pun">&gt;&gt;</span><span class="pln"> $HOME</span><span class="pun">/.</span><span class="pln">bashrc</span></code></li><li class="L4"><code class="lang-sh"><span class="pln">echo </span><span class="str">&#39;export PATH=$PATH:$GOROOT/bin:$GOPATH/bin&#39;</span><span class="pln"> </span><span class="pun">&gt;&gt;</span><span class="pln"> $HOME</span><span class="pun">/.</span><span class="pln">bashrc</span></code></li><li class="L5"><code class="lang-sh"><span class="pln">source $HOME</span><span class="pun">/.</span><span class="pln">bashrc</span></code></li></ol></pre>
<h2 id="h2--gogs"><a name="安装 Gogs" class="reference-link"></a><span class="header-link octicon octicon-link"></span>安装 Gogs</h2><p>常用的安装方式：</p>
<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code class="lang-sh"><span class="com"># 下载并安装依赖</span></code></li><li class="L1"><code class="lang-sh"><span class="pln">$ go get </span><span class="pun">-</span><span class="pln">u github</span><span class="pun">.</span><span class="pln">com</span><span class="pun">/</span><span class="pln">gogits</span><span class="pun">/</span><span class="pln">gogs</span></code></li><li class="L2"><code class="lang-sh"></code></li><li class="L3"><code class="lang-sh"><span class="com"># 构建主程序</span></code></li><li class="L4"><code class="lang-sh"><span class="pln">$ cd $GOPATH</span><span class="pun">/</span><span class="pln">src</span><span class="pun">/</span><span class="pln">github</span><span class="pun">.</span><span class="pln">com</span><span class="pun">/</span><span class="pln">gogits</span><span class="pun">/</span><span class="pln">gogs</span></code></li><li class="L5"><code class="lang-sh"><span class="pln">$ go build</span></code></li></ol></pre>
<h3 id="h3--code-develop-code-"><a name="构建 &lt;code&gt;develop&lt;/code&gt; 分支版本" class="reference-link"></a><span class="header-link octicon octicon-link"></span>构建 <code>develop</code> 分支版本</h3><p>如果您想要安装 <code>develop</code>（或其它）分支版本，则可以通过以下命令：</p>
<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code class="lang-sh"><span class="pln">$ mkdir </span><span class="pun">-</span><span class="pln">p $GOPATH</span><span class="pun">/</span><span class="pln">src</span><span class="pun">/</span><span class="pln">github</span><span class="pun">.</span><span class="pln">com</span><span class="pun">/</span><span class="pln">gogits</span></code></li><li class="L1"><code class="lang-sh"><span class="pln">$ cd $GOPATH</span><span class="pun">/</span><span class="pln">src</span><span class="pun">/</span><span class="pln">github</span><span class="pun">.</span><span class="pln">com</span><span class="pun">/</span><span class="pln">gogits</span></code></li><li class="L2"><code class="lang-sh"></code></li><li class="L3"><code class="lang-sh"><span class="com"># 请确保没有使用 “https://github.com/gogits/gogs.git”</span></code></li><li class="L4"><code class="lang-sh"><span class="pln">$ git clone </span><span class="pun">--</span><span class="pln">depth</span><span class="pun">=</span><span class="lit">1</span><span class="pln"> </span><span class="pun">-</span><span class="pln">b develop https</span><span class="pun">://</span><span class="pln">github</span><span class="pun">.</span><span class="pln">com</span><span class="pun">/</span><span class="pln">gogits</span><span class="pun">/</span><span class="pln">gogs</span></code></li><li class="L5"><code class="lang-sh"><span class="pln">$ cd gogs</span></code></li><li class="L6"><code class="lang-sh"><span class="pln">$ go build</span></code></li></ol></pre>
<h3 id="h3-u6D4Bu8BD5u5B89u88C5"><a name="测试安装" class="reference-link"></a><span class="header-link octicon octicon-link"></span>测试安装</h3><p>您可以通过以下方式检查 Gogs 是否可以正常工作：</p>
<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code class="lang-sh"><span class="pln">cd $GOPATH</span><span class="pun">/</span><span class="pln">src</span><span class="pun">/</span><span class="pln">github</span><span class="pun">.</span><span class="pln">com</span><span class="pun">/</span><span class="pln">gogits</span><span class="pun">/</span><span class="pln">gogs</span></code></li><li class="L1"><code class="lang-sh"><span class="pun">./</span><span class="pln">gogs web</span></code></li></ol></pre>
<p>如果您没有发现任何错误信息，则可以使用 <code>Ctrl-C</code> 来终止运行。</p>
<h3 id="h3-u4F7Fu7528u6807u7B7Eu6784u5EFA"><a name="使用标签构建" class="reference-link"></a><span class="header-link octicon octicon-link"></span>使用标签构建</h3><p>Gogs 默认并没有支持一些功能，这些功能需要在构建时明确使用构建标签（<a href="https://golang.org/pkg/go/build/#hdr-Build_Constraints">build tags</a>）来支持。</p>
<p>目前使用标签构建的功能如下：</p>
<ul>
<li><code>sqlite3</code>：SQLite3 数据库支持</li><li><code>pam</code>：PAM 授权认证支持</li><li><code>cert</code>：生成自定义证书支持</li><li><code>miniwinsvc</code>：Windows 服务内置支持（或者您可以使用 NSSM 来创建服务）</li></ul>
<p>例如，您需要支持以上所有功能，则需要先删除 <code>$GOPATH/pkg/{GOOS_GOARCH}/github.com/gogits/gogs</code> 目录，然后执行以下命令：</p>
<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code class="lang-sh"><span class="pln">$ go get </span><span class="pun">-</span><span class="pln">u </span><span class="pun">-</span><span class="pln">tags </span><span class="str">&#34;sqlite pam cert&#34;</span><span class="pln"> github</span><span class="pun">.</span><span class="pln">com</span><span class="pun">/</span><span class="pln">gogits</span><span class="pun">/</span><span class="pln">gogs</span></code></li><li class="L1"><code class="lang-sh"><span class="pln">$ cd $GOPATH</span><span class="pun">/</span><span class="pln">src</span><span class="pun">/</span><span class="pln">github</span><span class="pun">.</span><span class="pln">com</span><span class="pun">/</span><span class="pln">gogits</span><span class="pun">/</span><span class="pln">gogs</span></code></li><li class="L2"><code class="lang-sh"><span class="pln">$ go build </span><span class="pun">-</span><span class="pln">tags </span><span class="str">&#34;sqlite pam cert&#34;</span></code></li></ol></pre>
<p>安装完成后可继续参照 [配置与运行]。</p>

    </div>
</body>
</html>